package com.tonann.app.controller;

import com.tonann.app.domain.AppFaultRule;
import com.tonann.app.service.IAppFaultRuleService;
import com.tonann.common.annotation.Log;
import com.tonann.common.core.controller.BaseController;
import com.tonann.common.core.domain.AjaxResult;
import com.tonann.common.core.page.TableDataInfo;
import com.tonann.common.enums.BusinessType;
import com.tonann.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 故障诊断规则Controller
 * 
 * @author tonann
 * @date 2022-10-15
 */
@RestController
@RequestMapping("/app/rule")
public class AppFaultRuleController extends BaseController
{
    @Autowired
    private IAppFaultRuleService appFaultRuleService;

    /**
     * 查询故障诊断规则列表
     */
    @PreAuthorize("@ss.hasPermi('app:rule:list')")
    @GetMapping("/list")
    public TableDataInfo list(AppFaultRule appFaultRule)
    {
        startPage();
        List<AppFaultRule> list = appFaultRuleService.selectAppFaultRuleList(appFaultRule);
        return getDataTable(list);
    }

    /**
     * 导出故障诊断规则列表
     */
    @PreAuthorize("@ss.hasPermi('app:rule:export')")
    @Log(title = "故障诊断规则", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AppFaultRule appFaultRule)
    {
        List<AppFaultRule> list = appFaultRuleService.selectAppFaultRuleList(appFaultRule);
        ExcelUtil<AppFaultRule> util = new ExcelUtil<AppFaultRule>(AppFaultRule.class);
        util.exportExcel(response, list, "故障诊断规则数据");
    }

    /**
     * 获取故障诊断规则详细信息
     */
    @PreAuthorize("@ss.hasPermi('app:rule:query')")
    @GetMapping(value = "/{faultRuleId}")
    public AjaxResult getInfo(@PathVariable("faultRuleId") Long faultRuleId)
    {
        return AjaxResult.success(appFaultRuleService.selectAppFaultRuleByFaultRuleId(faultRuleId));
    }

    /**
     * 新增故障诊断规则
     */
    @PreAuthorize("@ss.hasPermi('app:rule:add')")
    @Log(title = "故障诊断规则", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AppFaultRule appFaultRule)
    {
        appFaultRule.setCreateBy(getUsername());
        return toAjax(appFaultRuleService.insertAppFaultRule(appFaultRule));
    }

    /**
     * 修改故障诊断规则
     */
    @PreAuthorize("@ss.hasPermi('app:rule:edit')")
    @Log(title = "故障诊断规则", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AppFaultRule appFaultRule)
    {
        appFaultRule.setUpdateBy(getUsername());
        return toAjax(appFaultRuleService.updateAppFaultRule(appFaultRule));
    }

    /**
     * 删除故障诊断规则
     */
    @PreAuthorize("@ss.hasPermi('app:rule:remove')")
    @Log(title = "故障诊断规则", businessType = BusinessType.DELETE)
	@DeleteMapping("/{faultRuleIds}")
    public AjaxResult remove(@PathVariable Long[] faultRuleIds)
    {
        return toAjax(appFaultRuleService.deleteAppFaultRuleByFaultRuleIds(faultRuleIds));
    }
}
